package com.hzy.fivepalace.db.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.hzy.fivepalace.common.type.VipTypeEnums;
import com.hzy.fivepalace.db.dao.DtsGoodsProductMapper;
import com.hzy.fivepalace.db.dao.ex.GoodsProductMapper;
import com.hzy.fivepalace.db.domain.DtsGoodsProduct;
import com.hzy.fivepalace.db.domain.DtsGoodsProductExample;
import com.hzy.fivepalace.db.domain.DtsUser;
import com.hzy.fivepalace.db.domain.DtsVipConfig;
import com.hzy.fivepalace.db.vo.wx.WxGoodsSpecificationVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DtsGoodsProductService {
    @Resource
    private DtsGoodsProductMapper dtsGoodsProductMapper;

    @Resource
    private GoodsProductMapper goodsProductMapper;


    @Autowired
    private DtsVipConfigService dtsVipConfigService;

    public List<DtsGoodsProduct> queryByGid(Integer gid) {
        DtsGoodsProductExample example = new DtsGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
        return dtsGoodsProductMapper.selectByExample(example);
    }



    /**
     * 获取商品规格对应的数量和价格
     *
     * @param goodsId
     * @return
     */
    public List<specificationVo> getGoodsProduct(Integer goodsId,DtsUser user,boolean isAdmin) {
        //根据商品id获取规格
        List<WxGoodsSpecificationVo> specificationList = goodsProductMapper.getSpecificationByGoodsId(goodsId);
        if (!CollectionUtils.isEmpty(specificationList)) {//规格不为空，根据规格名称分组
            Map<String, specificationVo> map = new HashMap<>();
            List<specificationVo> specificationVoList = new ArrayList<>();
            for (WxGoodsSpecificationVo goodsSpecification : specificationList) {
                if(null!=user&&!user.getVipType().equals(0)&&!isAdmin){
                    DtsVipConfig scaleConfig=dtsVipConfigService.getGoodsExtendScale(user.getVipType(),goodsSpecification.getProductId());
                    if(null!=scaleConfig&&null!=scaleConfig.getGoodsOneLevelExtendScale()){
                        BigDecimal goodsOneLevelExtendAmount=goodsSpecification.getPrice().multiply(scaleConfig.getGoodsOneLevelExtendScale());
                        if(goodsOneLevelExtendAmount.compareTo(new BigDecimal("0.01"))>=0){
                            goodsSpecification.setCommission(goodsOneLevelExtendAmount);
                        }
                    }
                }
                if(isAdmin){
                    DtsVipConfig scaleConfig1=dtsVipConfigService.getGoodsExtendScale(VipTypeEnums.TYPE1.getId(),goodsSpecification.getProductId());
                    if(null!=scaleConfig1){
                        goodsSpecification.setOneLevelExtendScaleVipType1(scaleConfig1.getGoodsOneLevelExtendScale());
                        goodsSpecification.setTwoLevelExtendScaleVipType1(scaleConfig1.getGoodsTwoLevelExtendScale());
                    }
                    DtsVipConfig scaleConfig2=dtsVipConfigService.getGoodsExtendScale(VipTypeEnums.TYPE2.getId(),goodsSpecification.getProductId());
                    if(null!=scaleConfig2){
                        goodsSpecification.setOneLevelExtendScaleVipType2(scaleConfig2.getGoodsOneLevelExtendScale());
                        goodsSpecification.setTwoLevelExtendScaleVipType2(scaleConfig2.getGoodsTwoLevelExtendScale());
                    }
                    DtsVipConfig scaleConfig3=dtsVipConfigService.getGoodsExtendScale(VipTypeEnums.TYPE3.getId(),goodsSpecification.getProductId());
                    if(null!=scaleConfig3){
                        goodsSpecification.setOneLevelExtendScaleVipType3(scaleConfig3.getGoodsOneLevelExtendScale());
                        goodsSpecification.setTwoLevelExtendScaleVipType3(scaleConfig3.getGoodsTwoLevelExtendScale());
                    }
                    DtsVipConfig scaleConfig4=dtsVipConfigService.getGoodsExtendScale(VipTypeEnums.TYPE4.getId(),goodsSpecification.getProductId());
                    if(null!=scaleConfig4){
                        goodsSpecification.setOneLevelExtendScaleVipType4(scaleConfig4.getGoodsOneLevelExtendScale());
                        goodsSpecification.setTwoLevelExtendScaleVipType4(scaleConfig4.getGoodsTwoLevelExtendScale());
                    }
                }

                String specification = goodsSpecification.getSpecificationName();
                specificationVo goodsSpecificationVo = map.get(specification);
                if (goodsSpecificationVo == null) {
                    goodsSpecificationVo = new specificationVo();
                    goodsSpecificationVo.setName(specification);
                    List<WxGoodsSpecificationVo> valueList = new ArrayList<>();
                    valueList.add(goodsSpecification);
                    goodsSpecificationVo.setValueList(valueList);
                    map.put(specification, goodsSpecificationVo);
                    specificationVoList.add(goodsSpecificationVo);
                } else {
                    List<WxGoodsSpecificationVo> valueList = goodsSpecificationVo.getValueList();
                    valueList.add(goodsSpecification);
                }
            }
            return specificationVoList;
        }
        return new ArrayList<>();
    }

    public class specificationVo {
        private String name;
        private List<WxGoodsSpecificationVo> valueList;

        @SuppressWarnings("unused")
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<WxGoodsSpecificationVo> getValueList() {
            return valueList;
        }

        public void setValueList(List<WxGoodsSpecificationVo> valueList) {
            this.valueList = valueList;
        }

    }


    public DtsGoodsProduct selectById(Integer id) {
        DtsGoodsProductExample example = new DtsGoodsProductExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return dtsGoodsProductMapper.selectOneByExampleSelective(example);
    }

    public DtsGoodsProduct findById(Integer id) {
        return dtsGoodsProductMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer id) {
        dtsGoodsProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(DtsGoodsProduct goodsProduct) {
        goodsProduct.setAddTime(LocalDateTime.now());
        goodsProduct.setUpdateTime(LocalDateTime.now());
        dtsGoodsProductMapper.insertSelective(goodsProduct);
    }

    public int count() {
        DtsGoodsProductExample example = new DtsGoodsProductExample();
        example.or().andDeletedEqualTo(false);
        return (int) dtsGoodsProductMapper.countByExample(example);
    }

    public void deleteByGid(Integer gid) {
        DtsGoodsProductExample example = new DtsGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid);
        dtsGoodsProductMapper.logicalDeleteByExample(example);
    }

    public int addStock(Integer id, Short num) {
        return goodsProductMapper.addStock(id, num);
    }

    public int addBrowse(Integer id, Short num) {
        return goodsProductMapper.addBrowse(id, num);// 新增商品流量量
    }

    public int reduceStock(Integer id,Short num) {
       // goodsProductMapper.addSales(goodsId, num);// 每次需将商品的销售量加下
        return goodsProductMapper.reduceStock(id, num);
    }
}
